/*
 * JsonRpcServletConfig
 * 
 * Copyright Aldrin Edison Baroi 2006 
 * 
 * Aldrin Edison Baroi <aldrin91011@yahoo.com>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 * 
 */
package org.charless.spring.jsonrpc;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import com.metaparadigm.jsonrpc.JSONRPCServlet;

import cx.ath.choisnet.util.iterator.IteratorHelper;

/**
 * JSON-RPC servlet is not configured using web.xml and ServletConfig provided
 * by the container is immutable. JsonRpcServletConfig wraps ServletConfig
 * provided by the container and adds JSON-RPC servlet specific configuration
 * information.
 * 
 * @author Aldrin E Baroi
 * 
 */
public class JsonRpcServletConfig implements ServletConfig
{
	private final JsonRpcServletContext jsonRpcServletContext;
	private final String servletName;
	private final Map<String,String> initParameters;

	/**
	 * 
	 */
	public JsonRpcServletConfig(JsonRpcServletContext jsonRpcServletContext,
			ServletContext servletContext)
	{
		super();
		this.jsonRpcServletContext = jsonRpcServletContext;
		this.servletName = JSONRPCServlet.class.getName();
		this.initParameters = new HashMap<String,String>();

		//Enumeration e = servletContext.getInitParameterNames();
		Enumeration<String> e = servletContext.getInitParameterNames();
		
		while (e.hasMoreElements())
		{
			String key = (String) e.nextElement();
			String val = servletContext.getInitParameter(key);
			this.initParameters.put(key, val);
		}
		// Disabling auto session bridge creation, since we are not using it
		this.initParameters.put("auto-session-bridge", "0");
		// Forcing keep alive
		this.initParameters.put("keepalive", "1");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletConfig#getServletName()
	 */
	@Override
	public String getServletName()
	{
		return /*new String(*/servletName/*)*/;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletConfig#getServletContext()
	 */
	@Override
	public ServletContext getServletContext()
	{
		return jsonRpcServletContext;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletConfig#getInitParameter(java.lang.String)
	 */
	@Override
	public String getInitParameter(String parameterName)
	{
		return /*(String)*/ initParameters.get(parameterName);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletConfig#getInitParameterNames()
	 */
	@Override
	public Enumeration<String> getInitParameterNames()
	{
		//return initParameters.keySet();
		return IteratorHelper.toEnumeration( initParameters.keySet().iterator() );
	}
}